<?php

namespace App\Service\Friend;

// use App\Constants\ErrorCode;

use App\Constants\ErrorCode;
use App\Model\Moment;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 推荐动态列表(发现宝藏跳转)
 */
class RecMomentListService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $page = $this->request->input('page', 1);
        $userProfile = UserProfile::getProfile($userId);
        $ret['list'] = [];
        //动态列表
        $this->pageNum = 10;
        $list = Moment::getImageList($page, $this->pageNum, 1 - $userProfile->gender);
        if (empty($list)) {
            return $this->setError(ErrorCode::NO_VALID_HOST, "暂未发现适合你的人，请稍后再试~");
        }

        $userIds = array_column($list, 'user_id');
        $profiles = [];
        $data = UserProfile::getUsersProfile($userIds);
        foreach ($data as $item) $profiles[$item['user_id']] = $item;
        // return $list;
        array_walk($list, function ($moment) use (&$ret, $profiles) {
            $item = Moment::buildItem($moment, $profiles[$moment['user_id']] ?? null);
            $ret['list'][] = [
                'moment_id' => $item['moment_id'],
                'user_id' => $item['user_id'],
                'age' => $item['age'],
                'gender' => $item['gender'],
                'nick_name' => $item['nick_name'],
                'words' => $item['words'],
                'images' => [$item['images'][0]],
            ];
        });
        return $ret;
    }
}
